Page({
	request(url){ //发起请求，返回一个Promise
		return new Promise((resolve, reject)=>{
			wx.request({
				url,
				
				success: r => {
					resolve(r.data) //成功时返回的数据
				},
				fail: e=>{
					resolve(null) //null表示请求失败
				}
			})
		})
	},
	play(e){ //播放点击的视频
		let index=e.currentTarget.dataset.index //点击视频的索引号
		console.log(index)
		wx.navigateTo({//跳转到播放页面去播放视频
			url: '/pages/jqq/player?index='+index
		})
	},
	menus(e) { //点击菜单项
		let menuIndex = e.target.dataset.index //点击菜单项的索引号
		let oldIndex = this.data.menuIndex //获取的是原来点击的菜单项的索引号
		let change = {
			menuIndex
		} //记录有变化的数据
		change['menus[' + oldIndex + '].selected'] = false //取消原来的菜单项选中效果
		change['menus[' + menuIndex + '].selected'] = true //设置最新点击菜单项选中效果
		this.setData(change)
	},
	async types(e) { //点击菜单对应的某类型
		let typeIndex = e.target.dataset.index //获取点击类型的索引号
		let menuIndex = this.data.menuIndex //当前菜单的索引号
		let menu = this.data.menus[menuIndex] //获取当前菜单项
		let type = menu.types[typeIndex] //获取当前类型项
		let oldIndex = menu.typeIndex //上次点击的类型索引号
		let change = {} //记录有变化的数据
		if (oldIndex != null) change['menus[' + menuIndex + '].types[' + oldIndex + '].selected'] = false
		change['menus[' + menuIndex + '].types[' + typeIndex + '].selected'] = true
		change['menus[' + menuIndex + '].typeIndex'] = typeIndex
		if(type.list==null){ //当前类型下没有视频数据时才会发起请求
			//https://jqqzx.cc/vodshow/class/Netflix/id/dongman.html
			let url = 'https://jqqzx.cc/vodshow/' + (type.id)+'/page/1.html' 
			console.log(url)
			let html= await this.request(url) //获取网页返回的html文本
			console.log(html)
			if(html!=null){ //成功获取数据
				let regex = /<a href="(\/vod[^"]+)" title="([^"]+)".+?data-original="([^"]+)"/gs
				let r, list = [] //用来存储找到的所有电影信息
				while (r = regex.exec(html)) {
					list.push({
						url: r[1], //视频超链接
						img: r[3], //图片地址
						title: r[2], //视频的标题
					})
				}
				change['menus[' + menuIndex + '].types[' + typeIndex + '].list'] = list
			}else{ //获取数据失败，可以给出提醒信息

			}
		}
		this.setData(change)
	},
	onHide(){ //页面进入后台时执行
		wx.setStorageSync('jqq', this.data)
	},
	onUnload(){ //页面退出时执行
		wx.setStorageSync('jqq', this.data)
	},
	onLoad(e){ //页面加载时执行
		let data=wx.getStorageSync('jqq')
		if(data)this.setData(data)
	},
	data: {
		menuIndex: 0,
		menus: [{
				menu: '电影',
				selected: true,
				types: [
					{ type: '动作片', id:'id/dongzuopian' },
					{ type: '喜剧片', id:'id/xijupian' },
					{ type: '爱情片', id:'id/aiqingpian' },
					{ type: '科幻片', id:'id/kehuanpian' },
					{ type: '恐怖片', id:'id/kongbupian' },
					{ type: '剧情片', id:'id/juqingpian' },
					{ type: '战争片', id:'id/zhanzhengpian' },
					{ type: '动画片', id:'id/donghuapian' },
				],
			},
			{
				menu: '电视剧',
				types: [
					{ type: '国产剧', id:'id/guochanju' },
					{ type: '港台剧', id:'id/gangtaiju' },
					{ type: '日韩剧', id:'id/rihanju' },
					{ type: '欧美剧', id:'id/oumeiju' },
					{ type: '泰国剧', id:'id/taiguoju' },
				],
			},
			{
				menu: '综艺',
				types: [
					{ type: 'Netflix', id:'class/Netflix/id/zongyi' },
					{ type: '真人秀', id:'class/%E7%9C%9F%E4%BA%BA%E7%A7%80/id/zongyi' },
					{ type: '音乐', id:'class/%E9%9F%B3%E4%B9%90/id/zongyi' },
					{ type: '喜剧', id:'class/%E5%96%9C%E5%89%A7/id/zongyi' },
					{ type: '脱口秀', id:'class/%E8%84%B1%E5%8F%A3%E7%A7%80/id/zongyi' },
					{ type: '文化', id:'class/%E6%96%87%E5%8C%96/id/zongyi' },
					{ type: '美食', id:'class/%E7%BE%8E%E9%A3%9F/id/zongyi' },
				]
			},
			{
				menu: '动漫',
				types: [
					{ type: 'Netflix', id:'class/Netflix/id/dongman' },
					{ type: '奇幻', id:'class/%E5%A5%87%E5%B9%BB/id/dongman' },
					{ type: '动作', id:'class/%E5%8A%A8%E4%BD%9C/id/dongman' },
					{ type: '科幻', id:'class/%E7%A7%91%E5%B9%BB/id/dongman' },
					{ type: '喜剧', id:'class/%E5%96%9C%E5%89%A7/id/dongman' },
					{ type: '冒险', id:'class/%E5%86%92%E9%99%A9/id/dongman' },
					{ type: '后宫', id:'class/%E5%90%8E%E5%AE%AB/id/dongman' },
					{ type: '爱情', id:'class/%E7%88%B1%E6%83%85/id/dongman' },
					{ type: '悬疑', id:'class/%E6%82%AC%E7%96%91/id/dongman' },
					{ type: '机战', id:'class/%E6%9C%BA%E6%88%98/id/dongman' },
					{ type: '战争', id:'class/%E6%88%98%E4%BA%89/id/dongman' },
					{ type: '其他', id:'class/%E5%85%B6%E4%BB%96/id/dongman' },
					
				]
			},
		],
	},
})